<%--
  Created by IntelliJ IDEA.
  User: dj
  Date: 2018-11-02
  Time: 14:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加数据</title>

    <%@ include file="/common/baseMeta.jsp" %>
    <link rel="stylesheet" href="/static/js/layui/css/layui.css" media="all">
    <script type="text/javascript" href="/static/js/layui/html5.min.js"></script>
    <script type="text/javascript" href="/static/js/layui/respond.min.js"></script>

    <script type="text/javascript" src="/static/js/common.js"></script>
    <script type="text/javascript" src="/static/js/dlcommon.js"></script>
    <link rel="stylesheet" href="/static/js/plugins/validationEngine/css/validationEngine.jquery.css" type="text/css"/>


    <script type="text/javascript">
        var uid = "${sysStaffEntity.uid}";//用户唯一标识
        var lname = "${sysStaffEntity.lname}"; //登录工号
        var uname = "${sysStaffEntity.uname}"; //用户名 
        var mid = $.random.getGuid();//唯一ID
        /*页面控件信息初始化*/
        $(function () {
            var PageMode = getQueryStringRegExp("PageMode");
            var ID = getQueryStringRegExp("ID");
            $("#Hjjcinfo").height(350);
            initHjjcinfo();
            //初始化查询参数
            $.getJSON("/commapiV2", {p_dm: "Sys_Dictionary_Item_Get", dic_name: "环境监测监测级别"}, function (data) {
                if (data.success) {
                    $('#jcjb').combobox({
                        data: data.Table,
                        valueField: 'code',
                        textField: 'name'
                    });
                    $('#jcjb').combobox('select', data.Table[0].code);
                }
            }, true);

            //初始化采样人所在科室
            $.getJSON("/commapiV2", {p_dm: "sys_ks_GetByUser", userid: lname}, function (data) {
                if (data.success) {
                    $('#szks').combobox({
                        data: data.Table,
                        valueField: 'code',
                        textField: 'name'
                    });
                    $(szks).combobox('select', data.Table[0].code);
                }
            }, true);


            var myDate = new Date();
            var cysj = new Date(myDate);
            $("#cysj").datebox('setValue', cysj.format("yyyy-MM-dd"));
            $("#cyr").textbox('setValue', uname);

            if (PageMode == "Edite") {
                BandHjjcinfo(ID);
                Editedata(ID);
            }
            else {
                BandHjjcinfo(mid);
            }
            $('#cysj').datebox().datebox('calendar').calendar({
                validator: function (date) {
                    var now = new Date();
                    var d2 = new Date(now.getFullYear(), now.getMonth(), now.getDate());
                    return date <= d2;
                }
            });
        })
        function initHjjcinfo() {
            $('#Hjjcinfo').datagrid({
                fit: false, //自动适屏功能
                singleSelect: true,//只显示一行
                pagination: false,//启用分页
                remoteSort: false,
                fitColumns: true,
                sortable: false, //启用排序列
                collapsible: true,
                idField: 'id',
                singleSelect: true,
                columns: [
                    [
                        {field: 'xh', title: '序号', width: '80px', align: 'center'},
                        {field: 'jcks', title: '检测科室', width: '120px', align: 'center'},
                        {field: 'jclx', title: '检验类型', width: '80px', align: 'center'},
                        {field: 'jyxm', title: '检验项目', width: '80px', align: 'center'},
                        {field: 'jcbb', title: '检验标本', width: '80px', align: 'center'},
                        {field: 'jcjz', title: '检测菌种', width: '120px', align: 'left'},
                        {field: 'md', title: '目的', width: '60px', align: 'center'},
                        {field: 'bz', title: '备注', width: '80px', align: 'center'},
                        {
                            field: 'operate',
                            title: '操作',
                            width: '70px',
                            align: 'left',
                            sortable: false,
                            halign: 'center',
                            formatter: function (value, row, index) {
                                var str = '<button type=\'button\' class=\'btn btn-primary bi-glyphicon\' onclick="editDic(\'' + row.id + '\')"><span class=\'glyphicon  glyphicon glyphicon-pencil\'></span></button>';
                                str += '<button type=\'button\' class=\'btn btn-danger bi-glyphicon\' onclick="delDic(\'' + row.id + '\')"><span class=\'glyphicon glyphicon glyphicon-remove\'></span></button>';
                                return str;
                            }
                        }
                    ]
                ],
            });
        }
        function editDic(id) {
            AddData('Edite');
        }
        function delDic(id) {
            if (confirm('您确定要删除吗？')) {
                $.getJSON("/commapiV2", {p_dm: "Hjjc_content_Del", ID: id}, function (data) {
                    if (data.success) {
                        loadList(mid);
                    }
                }, true);
            }
        }
        function BandHjjcinfo(ID) {

            //绑定环境监测信息
            $.getJSON("/commapiV2", {p_dm: "Hjjc_content_get", mid: ID}, function (data) {
                if (data.success) {
                    var griddata = {};
                    griddata.rows = data.Table;
                    griddata.total = data.totalcount;
                    $('#Hjjcinfo').datagrid('loadData', griddata);
                }
            }, true);
        }
        function AddData(PageMode) {

            var ID = getQueryStringRegExp("ID");
            var url = "HjjcItemAdd?mid=" + mid;
            if (PageMode == 'Edite') {
                var row = $('#Hjjcinfo').datagrid('getSelected');
                var url = "HjjcItemAdd?mid=" + ID + "&PageMode=Edite&id=" + row.id;
            }
            layer.open({
                type: 2,
                title: '添加项目',
                skin: 'layui-layer-demo', //样式类名
                shadeClose: true, //开启遮罩关闭
                shade: [0.3, '#000000'],
                maxmin: true, //开启最大化最小化按钮
                area: ['800px', '350px'],
                content: url,
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    var body = layer.getChildFrame('body', index);
                    var iframeWin = window[layero.find('iframe')[0]['name']];//得到iframe页的窗口对象，执行iframe页的方法：
                    iframeWin.SaveData();//调用子页面的方法，得到子页面返回的ids 

                }
            });
        }
        function SaveData() {
            var PageMode = getQueryStringRegExp("PageMode");
            var ID = getQueryStringRegExp("ID");
            var postdata = {
                HJJC_JCMAIN: {
                    CYR: PageMode == "Edite" ? $('#cyr').textbox('getText') : uname,
                    JCJB: $('#jcjb').combobox('getValue'),
                    ID: PageMode == "Edite" ? ID : mid,
                    CYDATE: $("#cysj").datetimebox('getValue'),
                    ZT: '未发送',
                    SZKS: $('#szks').combobox('getValue')
                }
            }

            if ($('#szks').combobox('getValue') == "") {
                alert("采样科室不能为空");
                return;
            }
            $.ajaxSetup({
                async: false
            });
            //环境监测录入时间限制
            var num = 0;
            $.getJSON("/commapiV2", {p_dm: "Sys_Dictionary_Item_Get", dic_name: "环境监测时间限制"}, function (data) {
                if (data.success) {
                    var num = parseInt(data.Table[0].code);

                }
            }, true);

            $.ajaxSetup({
                async: true
            });
            if ($("#cysj").datebox('getValue') == "") {
                alert("采样时间不能为空！");
                return;
            }
            var myDate = new Date();
            var cysj = new Date($("#cysj").datebox('getValue'))

            if (myDate.getDate() >= num && cysj.format("yyyy-MM") < myDate.format("yyyy-MM"))//10号后只能上报当月采样数据；
            {
                $("#cysj").datebox('setValue', null);
                alert("每月" + num + "号后只能上报本月采样数据！");
                return;
            }
            if (myDate.getDate() < num && cysj.getMonth() < myDate.getMonth() - 1)//10号前只能报当月及上月采样数据；
            {
                $("#cysj").datebox('setValue', null);
                alert("每月" + num + "号前只能上报上月及本月采样数据！");
                return;
            }

            dLong.postajax("/commapiV2?p_dm=HJJC_JCMAIN_Add", postdata, function (data) {
                alert("保存成功");
                var index = parent.layer.getFrameIndex(window.name);
                window.parent.loadList();//执行父页面的事件 
                parent.layer.close(index);
            })
        }
        function loadList(MID) {
            BandHjjcinfo(MID);
        }
        function Editedata(ID) {
            //获取环境监测信息
            $.getJSON("/commapiV2", {p_dm: "Hjjc_JCMain_GetByID", ID: ID}, function (data) {
                if (data.success) {
                    $("#cyr").textbox('setText', data.Table[0].cyr);
                    $("#cysj").textbox('setText', data.Table[0].cydate);
                    $('#jcjb').combobox('select', data.Table[0].jcjb);
                    //获取用户角色
                    $.getJSON("/commapiV2", {p_dm: "Sys_User_Roles_Get", userid: lname}, function (data) {
                        if (data.success) {
                            if (data.Table[0]["role_name"] == "管理员组" || data.Table[0]["role_name"] == "院感科组") {
                                $('#szks').combobox('enable');
                            }
                            else {

                                $('#szks').combobox('disable');
                            }
                        }
                    }, true);
                }
            }, true);
        }
    </script>

</head>
<body style="overflow-y: scroll; overflow-x: hidden">
<table class="layui-table" lay-size="sm">
    <tbody>
    <tr>
        <td>采样时间</td>
        <td>
            <input id="cysj" name="cysj" class="easyui-datebox"
                   data-options="required:false" style="width:170px">
        </td>
        <td>采样人</td>
        <td>
            <input id="cyr" name="cyr" class="easyui-textbox" data-options="readonly:true"
                   style="width:170px">
        </td>
        <td> 监测级别</td>
        <td>

            <select id="jcjb" name="jcjb" data-options="required:true" class="easyui-combobox"
                    style="width:170px">
            </select>
        </td>
    </tr>
    <tr>
        <td>所在科室</td>
        <td>
            <input id="szks" name="szks" class="easyui-combobox" data-options="required:true" style="width:170px">
        </td>
        <td colspan="4">
            <button class="layui-btn" onclick="AddData('Add')" style="float: right">添加项目</button>
        </td>
    <tr>
        <td colspan="6">
            <table id="Hjjcinfo" name="Hjjcinfo" class="datagrid" style="width:100%"></table>
        </td>
    </tr>
    </tbody>
</table>

</body>
</html>
